package com.alogic.jsoup;

import com.alogic.xscript.Logiclet;
import com.anysoft.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Element;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * html清理白名单
 * @since 1.6.13.13 [20200904 duanyy] <br>
 */
public interface WhiteList {
    /**
     * 获取指定的tag配置信息
     * @param name 名称
     * @return tag实例
     */
    public Tag getTag(String name);
    public WhiteList addTags(String...names);
    public WhiteList addTagAttrs(String tag,String...attrs);
    public WhiteList addTagExtendAttr(String tag,String name,String value);
    public WhiteList addTagEvent(String name, Logiclet event);


    /**
     * 虚基类
     */
    public static abstract class Abstract implements WhiteList, Configurable, XMLConfigurable {
        /**
         * a logger of slf4j
         */
        protected static final Logger LOG = LoggerFactory.getLogger(WhiteList.class);
        @Override
        public void configure(Element e, Properties p) {
            XmlElementProperties props = new XmlElementProperties(e,p);
            configure(props);
        }
    }

    /**
     * Tag定义
     */
    public static class Tag {
        /**
         * tag名称
         */
        protected String name;
        /**
         * 属性
         */
        protected Set<String> attrs = new HashSet<String>();

        protected List<Pair<String,String>> extAttrs = new ArrayList<Pair<String,String>>();

        protected Logiclet event = null;

        public Tag(String name){
            this.name = name;
        }

        public String getName(){
            return name;
        }

        public Logiclet getEvent(){
            return event;
        }

        public void setEvent(Logiclet event){
            this.event = event;
        }

        public boolean existAttr(String name){
            return attrs.contains(name);
        }

        public void addAttr(String name){
            attrs.add(name);
        }

        public void addExtAttr(String name,String value){
            extAttrs.add(new Pair.Default<String,String>(name,value));
        }

        public List<Pair<String,String>> getExtAttrs(){
            return extAttrs;
        }
    }
}
